<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
<!--    <link rel="stylesheet" href="../css/element-ui-lib-theme-chalk/index.css">   &lt;!&ndash; 这个css不能正常显示，下面的可以显示但是网速慢 &ndash;&gt;-->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../js/vue/vue@2.6.11.js"></script>
    <script src="../plugins/z/index.js"></script>
    <script src="../js/element-ui-lib/index.js"></script>
<!--        <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body class="hold-transition">
<div class="content-header">
    <h1>系统设置<small>菜单管理</small></h1>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
</div>
<div id="lazyLoadTable">
    <div class="filter-container">
        <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"
                  @keyup.enter.native="handleFilter"></el-input>
        <el-button @click="queryMenu" class="dalfBut">查询</el-button>
        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
    </div>
    <!-- 新建菜单项弹层 -->
    <div class="add-form">
        <el-dialog title="新建菜单项" :visible.sync="dialogFormVisible4Create">
            <el-form label-position="right" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="菜单名称">
                            <el-input v-model="formData.name"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="访问路径">
                            <el-input v-model="formData.linkUrl"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="路径级别">
                            <el-input v-model="formData.path"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="优先级">
                            <el-input v-model="formData.priority"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="自关联id">
                            <el-select v-model="formData.parentMenuId" >
                                <el-option v-for="menu in parentMenus" v-bind:label="menu.name +': '+ menu.id" v-bind:value="menu.id" :key="menu.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="菜单级别">
                            <el-input v-model="formData.level"  @blur="setParentMenus()"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="图标">
                            <el-input v-model="formData.icon" type="text"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="注意事项">
                            <el-input v-model="formData.description" type="textarea"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4Create = false">取消</el-button>
                <el-button type="primary" @click="handlerAdd()">确定</el-button>
            </div>
        </el-dialog>
    </div>
    <!-- 展示表格 -->
    <el-table
            :data="tableData"
            style="width: 100%"
            row-key="id"
            border
            lazy
            :load="load"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
                prop="name"
                label="菜单名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="linkUrl"
                label="访问路径"
                width="180">
        </el-table-column>
        <el-table-column
                prop="path"
                label="路径级别"
                width="180">
        </el-table-column>
        <el-table-column
                prop="parentMenuId"
                label="自关联Id"
                width="180">
        </el-table-column>
        <el-table-column
                prop="level"
                label="菜单级别"
                width="180">
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        type="primary"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>
    <!-- 编辑菜单项弹层 -->
    <div class="add-form">
        <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisible4Edit">
            <el-form label-position="right" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="菜单名称">
                            <el-input v-model="formData.name"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="访问路径">
                            <el-input v-model="formData.linkUrl"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="路径级别">
                            <el-input v-model="formData.path" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="优先级">
                            <el-input v-model="formData.priority"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="自关联id">
                            <el-select v-model="formData.parentMenuId" >
                                <el-option v-for="menu in parentMenus" v-bind:label="menu.name +': '+ menu.id" v-bind:value="menu.id" :key="menu.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="菜单级别">
                            <el-input v-model="formData.level" @blur="setParentMenus()"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="图标">
                            <el-input v-model="formData.remark" type="text"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="注意事项">
                            <el-input v-model="formData.description" type="textarea"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                <el-button type="primary" @click="handlerUpdate()">确定</el-button>
            </div>
        </el-dialog>
    </div>



</div>
<style>
    .demo-table-expand {
        font-size: 0;
    }

    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }

    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>


<script>
    var lazyLoadTable= new Vue({
        el: "#lazyLoadTable",
        data: {
            tableData: null,
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 0,
                queryString: null,
            },
            sonCache:{},
            dataList: [],//列表数据
            formData: {},//表单数据
            parentMenus: [],
            checkitemIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible4Create: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false//控制编辑窗口显示/隐藏
        },
        methods: {
            //分页查询一级菜单
            queryLevel1Menu(){
                axios({url:"/menu/findAllWithoutChildrenByPage.do",data:this.pagination,method:"post"}).then((res) => {
                    this.pagination.currentPage= res.data.pageNum;
                    this.pagination.total=res.data.total;
                    this.tableData=res.data.list;
                    for (let i = 0; i <  this.tableData.length; i++) {
                        this.tableData[i].hasChildren = true;
                    }
                });
            },
            load(tree, treeNode, resolve) {
                this.querySonMenuByParentMenuId(tree.id);
                setTimeout(() => {
                    resolve(lazyLoadTable.sonCache);
                }, 500);
            },
            //查询子级菜单
            querySonMenuByParentMenuId(parentId){
                axios({url:"/menu/getSonMenu.do?menuId="+parentId+"&queryString="+lazyLoadTable.pagination.queryString,method:"get"}).then((res) => {
                    for (let i = 0; i < res.data.length; i++) {
                        res.data[i].hasChildren=true;
                    }
                    lazyLoadTable.sonCache=res.data;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.parentMenus=[];
            },
            //按条件分页查询
            queryWithCondition(){
                //null字符串，就是普通分页查询
                if (!this.pagination.queryString) {
                    this.queryLevel1Menu();
                    return;
                }
                //封装分页查询条件
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送ajax请求，进程条件分页查询
                axios({url:"/menu/queryMenuWithCondition.do", data:param,method:"post"}).then((res) => {
                    console.log(res);
                    this.pagination.total = res.data.total;
                    this.tableData=res.data.list;
                });
            },
            queryMenu() {
                this.pagination.currentPage=1;
                this.queryWithCondition();
            },
            // 弹出添加窗口,查询一级菜单作下拉框选项
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible4Create = true;//弹出窗口
                this.setParentMenus();
            },
            //添加提交
            handlerAdd() {
                axios({url:"/menu/create.do",data:this.formData,method:"post"}).then((res) => {
                    if (res.status != 200) {
                        alert("request failed");
                        return;
                    }
                    alert("添加成功！");
                    this.dialogFormVisible4Create=false;
                });
            },
            // 弹出编辑窗口
            handleEdit(index, row) {
                this.resetForm();
                this.dialogFormVisible4Edit=true;
                axios.get("/menu/getMenuById.do?id="+row.id).then((res)=>{
                    lazyLoadTable.formData=res.data;
                });
                this.setParentMenus()
            },
            handlerUpdate(){
                    axios({url:"/menu/update.do",method:"post",data:lazyLoadTable.formData}).then((res)=>{
                        if (res.data.msg == 'OK') {
                            alert("修改成功！");
                        }
                        lazyLoadTable.dialogFormVisible4Edit=false;
                        document.location.reload();
                    });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                //判断是否是条件查询
                if (lazyLoadTable.pagination.queryString) {
                    this.queryWithCondition();
                }else{
                    this.queryLevel1Menu();
                }
            },
            // 删除
            handleDelete(index,row) {
                //弹出确认框
                this.$confirm('你确定要删除当前数据吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送ajax请求，将id提交到Controller
                    axios({url: "/menu/delete.do?id=" + row.id, method: "get"}).then((res) => {
                        if (res.data.msg == 'OK') {
                            alert("删除成功");
                            document.location.reload();
                        } else {
                            alert("删除失败")
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message("已取消");
                });
            },
            //按照等级查询menu,填入自关联id下拉框
            setParentMenus(){
                let menuLevel=this.formData.level;
                let parentMenuLevel=1;
                if (!Number.isNaN(menuLevel)) {
                    parentMenuLevel=menuLevel-1;
                }
                axios({url:"/menu/findMenuByLevel.do?level="+parentMenuLevel,method:"get"}).then((res)=>{
                    lazyLoadTable.parentMenus=res.data;
                });
            }

        },
        created() {
            this.queryLevel1Menu();
        }
    });
</script>
</body>
</html>
